package sjtu.ist.warehouse4ws.service;

import java.util.ArrayList;
import java.util.List;

import sjtu.ist.warehouse4ws.dao.DepartmentDaoHibernate;
import sjtu.ist.warehouse4ws.dao.FileInfoDao;
import sjtu.ist.warehouse4ws.dao.cust.CustFileInfoDao;
import sjtu.ist.warehouse4ws.model.CustFileinfo;
import sjtu.ist.warehouse4ws.model.Fileinfo;
import sjtu.ist.warehouse4ws.tool.Constants;
import sjtu.ist.warehouse4ws.tool.PropertyHelper;
import sjtu.ist.warehouse4ws.vo.FullSearchVO;


public class FileInfoServiceImpl implements FileInfoService {
	private DepartmentDaoHibernate departmentDao;
	private FileInfoDao fileInfoDao;
	private CustFileInfoDao custFileInfoDao;

	public void setFileInfoDao(FileInfoDao fileInfoDao) {
		this.fileInfoDao = fileInfoDao;
	}
	
	public void setCustFileInfoDao(CustFileInfoDao custFileInfoDao) {
		this.custFileInfoDao = custFileInfoDao;
	}

	public void setDepartmentDao(DepartmentDaoHibernate departmentDao) {
		this.departmentDao = departmentDao;
	}

	@Override
	public List<Fileinfo> getFileInfoList() {
		// TODO Auto-generated method stub
		return (List<Fileinfo>)fileInfoDao.getAllFileInfos();
	}

	@Override
	public void saveFileInfo(Fileinfo fileInfo,int companyId) {
		// TODO Auto-generated method stub
		if(companyId==0){
			this.fileInfoDao.save(fileInfo);
		}else{
			CustFileinfo cf=new CustFileinfo();
			PropertyHelper.copyProperties(fileInfo, cf);
			custFileInfoDao.save(cf);
		}
	}

	@Override
	public List<Fileinfo> findFileInfoByName(String fileName) {
		// TODO Auto-generated method stub
		return fileInfoDao.findByName(fileName);
	}
	
	@Override
	public List<Fileinfo> findFileInfoByGroupId(String groupId) {
		// TODO Auto-generated method stub
		return fileInfoDao.findByGroupId(groupId);
	}
	
	@Override
	public List<Fileinfo> findFileInfoByProject(String project) {
		// TODO Auto-generated method stub
		return fileInfoDao.findByProject(project);
	}
	
	@Override
	public List<Fileinfo> searchFileInfo(String keyWord) {
		//模型普通搜索：根据关键字返回对应的文件信息
		System.out.println("FileInfoService");
		return fileInfoDao.findByKeyword(keyWord);
	}
	
	@Override
	public List<Fileinfo> searchDetailFileInfo(String keyWord1, String keyWord2, String keyWord3, 
												String keyWord4, String keyWord5, String keyWord6) {
		//模型高级搜索
		System.out.println("FileInfoService-Detail");
		return fileInfoDao.findDetailByKeyword(keyWord1,keyWord2,keyWord3,
												keyWord4,keyWord5,keyWord6);
	}
	@Override
	public void updateFileInfo(Fileinfo fileInfo,int companyId) {
		// TODO Auto-generated method stub
		if(companyId==Constants.ADMIN_COMPANY_ID){
			fileInfoDao.update(fileInfo);
		}else{
			CustFileinfo cfi=new CustFileinfo();
			PropertyHelper.copyProperties(fileInfo, cfi);
			custFileInfoDao.update(cfi);
		}		
	}
	
	@Override
	public Fileinfo getFileInfo(String id,int companyId) {
		// TODO Auto-generated method stub
		if(companyId==Constants.ADMIN_COMPANY_ID){
			return fileInfoDao.get(id);
		}else{
			return custFileInfoDao.get(id);
		}
	}
	
	//普通搜索：转换整个FileList的department信息
	@Override
	public List<FullSearchVO> getAllFullSearchResult(String keyWord) {
		List<Fileinfo> flist=fileInfoDao.findByKeyword(keyWord);
		List<FullSearchVO> fsvolist=new ArrayList<FullSearchVO>();
		for(Fileinfo f : flist){
			FullSearchVO fsvo=getFullSearchResult(f);
			fsvolist.add(fsvo);
		}
		return fsvolist;
	}
	
	//高级搜索：转换整个FileList的department信息
	@Override
	public List<FullSearchVO> getAllDetailSearchResult(String keyWord1, String keyWord2, String keyWord3,
			String keyWord4, String keyWord5, String keyWord6) {
		List<Fileinfo> flist=fileInfoDao.findDetailByKeyword(keyWord1,keyWord2,keyWord3,
				keyWord4,keyWord5,keyWord6);
		List<FullSearchVO> fsvolist=new ArrayList<FullSearchVO>();
		for(Fileinfo f : flist){
			FullSearchVO fsvo=getFullSearchResult(f);
			fsvolist.add(fsvo);
		}
		return fsvolist;
	}
	
	//获得vo
	private FullSearchVO getFullSearchResult(Fileinfo file){
		System.out.println("getFullSearchResult");
		Fileinfo fileInfo=fileInfoDao.get(file.getId());
		FullSearchVO fsvo=new FullSearchVO();
		fsvo.setId(fileInfo.getId());
		fsvo.setDepartment(departmentDao.findById(fileInfo.getDepartment()).getCnName());
		fsvo.setName(fileInfo.getName());
		fsvo.setProject(fileInfo.getProject());
		fsvo.setType(fileInfo.getType());
		fsvo.setVersion(fileInfo.getVersionId());
		return fsvo;
	}

	@Override
	public void deleteFileInfo(String id) {
		// TODO Auto-generated method stub
		fileInfoDao.delete(id);
	}

	@Override
	public List<Fileinfo> findFileId(String fileName, String project, int type) {
		// TODO Auto-generated method stub
		return fileInfoDao.getFileId(fileName,project,type);
	}

	public String getTypeById(int type) {
		switch (type) {
		case 1:
			return "process";
		case 2:
			return "function";
		case 3:
			return "department";
		case 4:
			return "domain";
		case 5:
			return "epc";
		case 6:
			return "permission";
		case 7:
			return "role";
		default:
			return "";
		}
	}
}
